package com.ciaojian.core.util;

import cn.hutool.core.util.StrUtil;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 在 cn.hutool.core.util.StrUtil 上扩展
 *
 * @author Atlakyn
 */
public class StringUtil extends StrUtil {
    /**
     * @Description: 将时间转换为时间戳
     * @Author: xiangchao
     * @Date: 2020/6/12 7:22 下午
     */
    public static String dateToStamp(String s) throws Exception {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public static List<String> getDates(String startDate, String endDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //起始日期
            Date start = sdf.parse(startDate);
            //结束日期
            Date end = sdf.parse(endDate);
            Date temp = start;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(start);
            List<String> list = new ArrayList<>();
            while (temp.getTime() < end.getTime()) {
                temp = calendar.getTime();
                String s = sdf.format(temp);
                list.add(s);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean checkMoney(String money) {
        // 金额正则,可以没有小数，小数最多不超过两位
        String regMoney = "\\d+(\\.\\d{1,2})?";
        Pattern pattern = Pattern.compile(regMoney);
        Matcher matcher = pattern.matcher(money);
        return matcher.matches();
    }

    /**
     * 获取真实IP
     *
     * @param request http 请求
     * @return 真实 ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StrUtil.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StrUtil.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }

    /**
     * 判断多个字符串其中任意一个是否为空
     *
     * @param str 字符串可变参数
     * @return 任何一个为空返回 true
     */
    public static boolean isHasEmpty(Object... str) {
        if (str == null) {
            return true;
        }
        for (Object s : str) {
            if (s == null || s.toString().length() == 0 || "null".equals(s)) {
                return true;
            }
            if (isEmpty(s.toString().trim())) {
                return true;
            }
        }
        return false;
    }
}
